package com.xdap.keymen.service.dayang;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Component;
import com.xdap.keymen.utils.OKHttpUtils;
import com.xdap.keymen.config.DayangClientConfig;

import java.util.HashMap;


@Component
public class DaYangTokenService {
    private final DayangClientConfig dayangClientConfig;
    private final OKHttpUtils okHttpUtils;
    public DaYangTokenService(DayangClientConfig dayangClientConfig, OKHttpUtils okHttpUtils) {
        this.dayangClientConfig = dayangClientConfig;
        this.okHttpUtils = okHttpUtils;
    }
    public String getBaseUrl() {
        return dayangClientConfig.getUrl();
    }

    public String getToken() {
        Gson gson = new GsonBuilder().create();
        HashMap<String, String> params = new HashMap<>();
        params.put("username", dayangClientConfig.getUsername());
        params.put("password", dayangClientConfig.getPassword());
        JsonObject res = gson.fromJson(okHttpUtils.doPostJson(dayangClientConfig.getUrl().concat("/api/authentication/login"),gson.toJson(params)), JsonObject.class);
        return res.get("accessToken").getAsString();
    }

}
